﻿
Option Strict On

Public Class FilteredListView

    Public Items As New List(Of FilteredListViewItem)

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
    End Sub

    Public Event SelectedIndexChanged()
    Public Event FilterTextChanged()

    Public Property CheckBoxes As Boolean
        Get
            Return lstView.CheckBoxes
        End Get
        Set(ByVal Value As Boolean)
            lstView.CheckBoxes = Value
        End Set
    End Property

    Public WriteOnly Property Colums As String()
        Set(ByVal Value() As String)
            lstView.Columns.Clear()
            For Each item As String In Value
                lstView.Columns.Add(item)
            Next
        End Set
    End Property

    Public WriteOnly Property FilterText As String
        Set(ByVal Value As String)
            txtZoeken.Text = Value
        End Set
    End Property

    Private Sub txtZoeken_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtZoeken.TextChanged
        RaiseEvent FilterTextChanged()
        FillListItems()
    End Sub

    Public Sub FillListItems()
        'remove existing items
        lstView.Items.Clear()
        'add new items
        If Items Is Nothing Then
            Items = New List(Of FilteredListViewItem)
        End If
        '--- search strings in listitems
        For Each item As FilteredListViewItem In Items
            'multiple string support
            Dim brokenString() As String = txtZoeken.Text.Split(CChar(" "))
            Dim length As Integer = brokenString.Length

            For Each brokenStringPart As String In brokenString
                'look if the item contains the searched string
                Dim found As Boolean = False
                For Each stringItem As String In item.TextArray
                    If Not found AndAlso stringItem.ToUpper.Contains(brokenStringPart.ToUpper) AndAlso Not brokenStringPart = "" Then
                        length -= 1 'search all parts
                        found = True
                    End If
                Next
                If brokenStringPart = "" Then
                    length -= 1
                End If
            Next
            If length < 1 Then
                'als alle delen van de gezochte string gevonden zijn
                lstView.Items.Add(item)
            End If
        Next
    End Sub

    Public ReadOnly Property SelectedItem As FilteredListViewItem
        Get
            If lstView.SelectedItems.Count > 0 Then
                Return CType(lstView.SelectedItems(0), FilteredListViewItem)
            End If
            Return Nothing
        End Get
    End Property

    Public ReadOnly Property SelectedObject As Object
        Get
            If lstView.SelectedItems.Count > 0 Then
                Return CType(lstView.SelectedItems(0), FilteredListViewItem).TaggedObject
            End If
            Return Nothing
        End Get
    End Property

    Private Sub FilteredListView_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub lstView_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstView.SelectedIndexChanged
        RaiseEvent SelectedIndexChanged()
    End Sub
End Class
